﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

class GenericHttp
{

    public string get(string url, string[] param = null)
    {
        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = string.Format("null={0}", ""); ;
        StringBuilder str = new StringBuilder();
        if (param != null)
        {
            foreach (String par in param)
            {
                str.Append(par + "&");
            }
            str.Remove(str.Length - 1, 1);
        }


        byte[] buffer = encoding.GetBytes(postData);

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

        // We use POST ( we can also use GET )
        myRequest.Method = "GET";

        // Set the content type to a FORM
        myRequest.ContentType = "application/x-www-form-urlencoded";

        // Get length of content
        myRequest.ContentLength = buffer.Length;

        // Get request stream
        Stream newStream = myRequest.GetRequestStream();

        // Send the data.
        newStream.Write(buffer, 0, buffer.Length);

        // Close stream
        newStream.Close();

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

        // Display the contents of the page to the console.
        Stream streamResponse = myHttpWebResponse.GetResponseStream();

        // Get stream object
        StreamReader streamRead = new StreamReader(streamResponse);

        Char[] readBuffer = new Char[256];

        // Read from buffer
        int count = streamRead.Read(readBuffer, 0, 256);

        String resultData = "";

        while (count > 0)
        {
            // get string
            resultData = resultData + new String(readBuffer, 0, count);

            // Read from buffer
            count = streamRead.Read(readBuffer, 0, 256);
        }





        // Release the response object resources.
        streamRead.Close();
        streamResponse.Close();

        // Close response
        myHttpWebResponse.Close();

        return resultData;

    }

    public void post(String url, String[] param)
    {
        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = string.Format("null={0}", ""); ;
        StringBuilder str = new StringBuilder();
        if (param != null)
        {
            foreach (String par in param)
            {
                str.Append(par + "&");
            }
            str.Remove(str.Length - 1, 1);
        }

        byte[] buffer = encoding.GetBytes(postData);

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

        // We use POST ( we can also use GET )
        myRequest.Method = "POST";

        // Set the content type to a FORM
        myRequest.ContentType = "application/x-www-form-urlencoded";

        // Get length of content
        myRequest.ContentLength = buffer.Length;

        // Get request stream
        Stream newStream = myRequest.GetRequestStream();

        // Send the data.
        newStream.Write(buffer, 0, buffer.Length);

        // Close stream
        newStream.Close();

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

        // Display the contents of the page to the console.
        Stream streamResponse = myHttpWebResponse.GetResponseStream();

        // Get stream object
        StreamReader streamRead = new StreamReader(streamResponse);

        Char[] readBuffer = new Char[256];

        // Read from buffer
        int count = streamRead.Read(readBuffer, 0, 256);

        // Release the response object resources.
        streamRead.Close();
        streamResponse.Close();

        // Close response
        myHttpWebResponse.Close();

    }
}

